//
// Created by Administrator on 2021/5/18.
//
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
class KthLargest {
private:
    int size;
    priority_queue<int,vector<int>,greater<>>pq; // 大堆顶
public:
    KthLargest(int k, vector<int>& nums) {
        size = k;
        for(const auto &x:nums) add(x);
    }

    int add(int val) {
        pq.push(val);
        if (pq.size() > size){
            pq.pop();
        }
        return pq.top();
    }
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */


int main()
{
    vector<int>v1{4,5,8,2};
    auto obj = new KthLargest(3,v1);
    cout << obj->add(3)<<endl;
    cout << obj->add(5)<<endl;
    cout << obj->add(10)<<endl;
    cout << obj->add(9)<<endl;
    cout << obj->add(4)<<endl;
    delete obj;
    return 0;
}